// Project name:JavaRedis
// Creator:muhong
// Date time:2022/12/9,10:34 下午
// Name:EchoServer

package org.gmh.server.v2;

import org.gmh.channel.Channel;
import org.gmh.channel.ChannelFuture;
import org.gmh.channel.ChannelHandler;
import org.gmh.channel.EventLoopGroup;
import org.gmh.channel.nio.NioEventLoopGroup;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

public class EchoServer {

    private final static Logger logger = LoggerFactory.getLogger(EchoServer.class);

    public static void main(String[] args) {
        // NioEventLoopGroup 是多线程的，不同的客户端socket可能被注册到不同EventLoop中
        EventLoopGroup bossGroup = new NioEventLoopGroup(1);
        EventLoopGroup childGroup = new NioEventLoopGroup(1);

        ServerBootstrap bootstrap = new ServerBootstrap();
        try {
            List<ChannelHandler> handlers = new ArrayList<>();
            handlers.add(new EchoHandler());
            ChannelFuture future = bootstrap.childHandlers(handlers)
                    .group(bossGroup, childGroup).bind(9090);
            logger.info("server is listening port:9090");
            Channel channel = future.channel();
            channel.closeFuture().sync();
        } catch (Exception e) {
            e.printStackTrace();
            // 关闭两个group
            bossGroup.shutdownGracefully();
            childGroup.shutdownGracefully();
        }
    }
}
